package hadoop.ch06.v17124080106;
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class TestZKWatcher {
    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        listener();
        operate();

        Thread.sleep(20 * 1000);
    }
    public static void listener() throws IOException, KeeperException, InterruptedException{
        // 创建一个与服务器的连接
        ZooKeeper zk = new ZooKeeper(ZookeeperConstant.URL, 1000 * 1, new Watcher() {
            // 监控所有被触发的事件
            public void process(WatchedEvent event) {
                System.out.println("已经触发了2" + event.getType() + "事件！" + event);
            }
        });
        zk.exists("/17124080106/testChildPathThree", new Watcher(){
            public void process(WatchedEvent event) {
                System.out.println("已经触发了3" + event.getType() + "事件！" + event);
            }
        });
        zk.exists("/17124080106", true);
    }
    private static void operate() throws IOException, KeeperException, InterruptedException {
        ZooKeeper zk = new ZooKeeper(ZookeeperConstant.URL, 1000 * 1, new Watcher() {
            // 监控所有被触发的事件
            public void process(WatchedEvent event) {
                System.out.println("已经触发了" + event.getType() + "事件！");
            }
        });
        if(zk.exists("/17124080106", false) != null){
            zk.delete("/17124080106", -1);
        }
        // 创建一个目录节点
        zk.create("/17124080106", "watcher".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        // 创建一个子目录节点
        zk.create("/17124080106/testChildPathOne", "1234".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(new String(zk.getData("/17124080106", false, null)));

        // 取出子目录节点列表
        System.out.println(zk.getChildren("/17124080106", true));

//        // 修改子目录节点数据
        zk.setData("/17124080106/testChildPathOne", "abcd".getBytes(), -1);
        System.out.println("目录节点状态：[" + zk.exists("/17124080106", true) + "]");

//        // 创建另外一个子目录节点
//        zk.create("/17124080106/testChildPathTwo", "testChildDataTwo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//        System.out.println(new String(zk.getData("/17124080106/testChildPathTwo", true, null)));
//
//        // 创建另外一个子目录节点
//        zk.create("/17124080106/testChildPathThree", "testChildDataThree".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
//
//        System.out.println(new String(zk.getData("/17124080106/testChildPathThree", true, null)));

        // 删除子目录节点
//        zk.delete("/17124080106/testChildPathTwo", -1);
//        zk.delete("/17124080106/testChildPathOne", -1);

        // 关闭连接
        zk.close();
    }
}
